<?php
include_once('includes/item.php');
?>
<div class="container">
<div id="main-content-wrapper">
        <div class="breadcrumb">
            <a href="index.php">YaMe</a>
            <span>&nbsp;&nbsp;&rsaquo;&nbsp;&nbsp;</span>           
            <a href="#">Giỏ hàng/Thanh toán</a>
            <span>&nbsp;&nbsp;&rsaquo;&nbsp;&nbsp;</span>            
        </div>

        <div id="title">            
	        <h1>Giỏ hàng của bạn</h1>
        </div>
        <div>
            
            <table cellpadding="0" cellspacing="0" width="100%" id="yourcartdetails">
                <thead>
                    <tr>
                        <th>STT</th>
                        <th>Tên sản phẩm</th>
                        <th>Giá bán (1 SP)</th>
                        <th class="acenter">Số lượng đặt mua</th>
                        <th colspan="2" style="width:410px;">Thành tiền</th>
                    </tr>
                </thead>
				<?php
					if(isset($_COOKIE['items']))
					{
						$items_cookie=$_COOKIE['items'];
						$items = explode(';',$items_cookie);
						$id=0;
						foreach($items as $r)
						{
							if($r!=''){
								$id++;
								$buf=explode('-',$r);
								$item=new Item($buf[0],$buf[1]);
								echo '
				<tr id="r_'.$item->data['tag'].'-'.$item->data['id'].'">
                    <td align="center">'.$id.'</td>
                    <td>'.$item->data['name'].'</td>
                    <td id="price_'.$buf[0].'-'.$buf[1].'">'.$item->data['price'].'</td>
                    <td class="acenter">
                        <a class="minus" title="Giảm 1" href="javascript:;" rel="'.$item->data['tag'].'-'.$item->data['id'].'"><span><img src="./Theme/Images/less.png" alt="Giảm" /></span></a>
                        <b><span class="qty" id="'.$item->data['tag'].'-'.$item->data['id'].'">&nbsp;&nbsp;</span></b>
                        <a class="plus" title="Cộng thêm 1"  href="javascript:;" rel="'.$item->data['tag'].'-'.$item->data['id'].'"><span><img src="./Theme/Images/add.png" alt="Cộng" /></span></a>
                    </td>
                    <td>
                            <b><span id="s_'.$buf[0].'-'.$buf[1].'">'.($item->data['price'] * $buf[2]).'</span></b>
                    </td>
                    <td align="center" style="width:40px;"><a class="remove" href="javascript:;" rel="'.$item->data['tag'].'-'.$item->data['id'].'"><span><img src="./Theme/Images/delete.png" alt="Xóa" /></span></a></td>
                </tr>';
							}
						}
					}
				?>                     
                <tfoot>
                    <tr>
                        <td colspan="4" class="aright">Tổng cộng</td>
                        <td colspan="2"><b><span data-bind="text: total" id="total"></span></b></td>
                    </tr>
                    <!--tr>
                        <td colspan="4" align="right">Khuyến mãi</td>
                        <td colspan="2"><b><span data-bind="text: sales"></span></b></td>
                    </tr>
                    <tr>
                        <td colspan="4" align="right"><b>Tính tổng</b></td>
                        <td colspan="2"><b><span data-bind="text: lastTotal"></span></b></td>
                    </tr-->
                </tfoot>   
            </table>
			<br/>
			<br/>
		</div>
	    <div id="title">            
	        <h1>Thông tin liên lạc</h1>
        </div>
		<div>
			<form action="index.php?p=checkout" class="regisform" method="post">
							<table cellpadding="0" cellspacing="0" class="form">                                                    
								<tr>
									<td class="label">Tên của bạn *</td>
									<td class="detail">
										<input class="textbox"  id="Username" name="Username" type="text" value="" />
										<span class="field-validation-valid" data-valmsg-for="Username" data-valmsg-replace="true"></span>
									</td>
								</tr>
								<tr>
									<td class="label">Số điện thoại *</td>
									<td>
										<input class="textbox" id="Phone" name="Phone" type="text" value="" />
										<span class="field-validation-valid" data-valmsg-for="Phone" data-valmsg-replace="true"></span>
									</td>
								</tr>
								<tr>
									<td class="label">Email</td>
									<td>
										<input class="textbox" id="Email" name="Email" type="text" value="" />
										<span class="field-validation-valid" data-valmsg-for="Email" data-valmsg-replace="true"></span>
									</td>
								</tr>
								
								<tr>
									<td class="label">Địa chỉ</td>
									<td>
										<input class="textbox" id="Address" name="Address" type="text" value="" />                                    
									</td>
								</tr>
								<tr>
									<td></td>
									<td>Trong trường hợp địa chỉ nhận hàng là địa chỉ khác, vui lòng điền vào thông tin bên dưới.</td>
								</tr>
								<tr>
									<td class="label">Địa chỉ giao/nhận hàng</td>
									<td>
										<input class="textbox" id="ShippingAddress" name="ShippingAddress" type="text" value="" />
									</td>
								</tr>
								<tr>
									<td></td>
									<td>                                        
										<input type="submit" id="sendCheckout" class="button narrow-aleft" value="Gửi đơn đặt hàng" />
									</td>
								</tr>
							</table>                            
			</form>  
		</div>
</div>
</div>
<script type="text/javascript">
	$('#sendCheckout').click(function(){
		SetCookie('items','');
	});
	function getTotalOrder()
	{
		buf=GetCookie('items');
		items=buf.split(';');
		var total=0;
		for(i=0;i<items.length-1;i++){
			buf1=items[i].split('-');
			total+=parseInt($('#s_'+buf1[0]+'-'+buf1[1]).text());
		}
		$('#total').text(total);
	}
	$(document).ready(function(){
		buf=GetCookie('items');
		items=buf.split(';');
		for(i=0;i<items.length-1;i++){
				buf1=items[i].split('-');
				var k=parseInt(buf1[2]);
				$('#'+buf1[0]+'-'+buf1[1]).html('&nbsp;'+k+'&nbsp;');
		}
		getTotalOrder();
	});
    $(function () {
        $('.plus').click(function () {
            var _id = $(this).attr("rel");
			buf=GetCookie('items');
			items=buf.split(';');
			buf='';
			for(i=0;i<items.length-1;i++){
				if(items[i].indexOf(_id)==0)
				{
					buf1=items[i].split('-');
					var k=parseInt(buf1[2])+1;
					items[i]=buf1[0]+'-'+buf1[1]+'-'+k;
					$('#'+_id).html('&nbsp;'+k+'&nbsp;');
					var price=parseInt($('#price_'+buf1[0]+'-'+buf1[1]).text());
					$('#s_'+buf1[0]+'-'+buf1[1]).html((k*price));
				}
				buf+=items[i]+';';
			}
			SetCookie('items',buf);
			getTotalOrder();
        });
		
        $('.minus').click(function () {
            var _id = $(this).attr("rel");
			buf=GetCookie('items');
			items=buf.split(';');
			buf='';
			for(i=0;i<items.length-1;i++){
				if(items[i].indexOf(_id)==0)
				{
					buf1=items[i].split('-');
					var k=parseInt(buf1[2])-1;
					k=k<1?1:k;
					items[i]=buf1[0]+'-'+buf1[1]+'-'+k;
					$('#'+_id).html('&nbsp;'+k+'&nbsp;');
					var price=parseInt($('#price_'+buf1[0]+'-'+buf1[1]).text());
					$('#s_'+buf1[0]+'-'+buf1[1]).html((k*price));
				}
				buf+=items[i]+';';
			}
			SetCookie('items',buf);
			getTotalOrder();
			
        });

        $('.remove').click(function () {
            var _id = $(this).attr("rel");
            $('#r_' + _id).hide();
			buf=GetCookie('items');
			items=buf.split(';');
			buf='';
			for(i=0;i<items.length-1;i++)
				if(items[i].indexOf(_id)<0) buf+=items[i]+';';
			SetCookie('items',buf);
			getTotalOrder();
			var k=parseInt($('#spcart').text());
			$('#spcart').text(k-1);
        });
    });

</script>
